//
//  KLAppViewModel.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/16.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "KLAppViewModel.h"
#import "KLRouter.h"

@interface KLAppViewModel()

@property(nonatomic,strong)NSMutableDictionary *notify_cmd;

@end

@implementation KLAppViewModel
- (instancetype)init{
    if(self = [super init]){
        _notify_cmd = [[NSMutableDictionary alloc]init];
    }
    return self;
}

- (void)configure{
    
}

- (void)registerCmd:(RACCommand*)cmd  forNotification:(NSString*)notification{
    NSMutableArray *disposbaleList = _notify_cmd[notification];
    if(!disposbaleList){
        disposbaleList = [[NSMutableArray alloc]init];
    }

    RACDisposable *disposable = [[[NSNotificationCenter defaultCenter]rac_addObserverForName:notification object:nil]subscribeNext:^(id x) {
        [cmd execute:x];
    }];
    
    if(disposable){
        [disposbaleList addObject:disposable];
        _notify_cmd[notification] = disposbaleList;
    }
}

- (void)unregisterCmd:(NSString*)notification{
    NSMutableArray *disposbaleList = _notify_cmd[notification];
    [disposbaleList enumerateObjectsUsingBlock:^(RACDisposable *disposable, NSUInteger idx, BOOL *stop) {
        [disposable dispose];
    }];
}
- (BOOL)openUrl:(NSURL *)url fromApplication:(UIApplication *)application{
    return [[KLRouter sharedInstance] handleUrl:url];
}


@end
